package pro.lepers.portfolio.view {
	import fr.digitas.flowearth.conf.Conf;
	import fr.digitas.flowearth.event.NodeEvent;
	import fr.digitas.flowearth.mvc.address.structs.INode;
	import fr.digitas.flowearth.mvc.address.structs.system.nodeSystem;

	import pro.lepers.portfolio.event.ContentEvent;
	import pro.lepers.portfolio.view.contents.AbstractContent;
	import pro.lepers.portfolio.view.contents.contact.Contact_FC;
	import pro.lepers.portfolio.view.contents.portfolio.Portfolio;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;

	/**
	 * @author plepers
	 */
	public class Content extends Sprite {

		public function Content () {
			_imports();
			_datas = Conf.getDatas( "contents" );
			addEventListener( Event.ADDED_TO_STAGE, onAdded );
			addEventListener( Event.REMOVED_FROM_STAGE, onRemoved );
		}


		private function onAdded( e : Event ) : void {
			_pNode = nodeSystem.getDefaultDevice();
			_pNode.addEventListener( NodeEvent.CHILD_CHANGE, onChildChange );
			onChildChange( null );
			
			stage.addEventListener( Event.RESIZE, onResize );
			onResize();
		}


		private function onRemoved( e : Event ) : void {
			_pNode.removeEventListener( NodeEvent.CHILD_CHANGE, onChildChange, true );
			_pNode = null;
			stage.removeEventListener( Event.RESIZE, onResize );
		}
		
		
		private function onResize (event : Event = null ) : void {
			if( _content )
				_content.setSize( stage.stageWidth, stage.stageHeight );
		}

		
		private function onChildChange (event : NodeEvent) : void {
			
			if( _content ) {
				if( _content.node == _pNode.getCurrentChild() )
					return;
				_content.addEventListener(ContentEvent.CLOSED, onContentClosed );
				_oldContent = _content;
				_content = null;
				_oldContent.close( );
			}
			
			loadContent( );
		}

		private function onContentClosed (event : ContentEvent) : void {
			if( _oldContent == event.content ) {
				removeChild( _oldContent );
				_oldContent.dispose();
				_oldContent = null;
			}
			if( _content )
				addContent();
		}

		private function loadContent () : void {
			var cdatas : XML = getCurrentDatas();
			
			if( dloader ) 
				dloader.removeEventListener( Event.COMPLETE, onCDatasLoaded );
				
			dloader = new URLLoader( );
			dloader.addEventListener(Event.COMPLETE, onCDatasLoaded );
			dloader.load( new URLRequest( cdatas.durl.text() ) );
		}

		private function onCDatasLoaded (event : Event) : void {
			var cdatas : XML = getCurrentDatas();
			
			var cClass : Class = ApplicationDomain.currentDomain.getDefinition( cdatas.mcClass ) as Class;
			_content = new cClass( );
			_content.setNode( _pNode.getCurrentChild() );
			_content.setDatas( XML( Conf.solve( ( event.target as URLLoader ).data ) ) );
			
			if( ! _oldContent )
				addContent();
		}
		
		private function addContent() : void {
			addChild( _content );
			onResize();
			
		}

		private function getCurrentDatas () : XML {
			return _datas.content.( @id == _pNode.getCurrentChild().getId() )[0];
		}

		
		
		private var _content : AbstractContent;
		private var _oldContent : AbstractContent;

		private var _pNode : INode;
		
		private var _datas : XML;
		
		private var dloader : URLLoader;




		private function _imports () : void {
			Portfolio;
			Contact_FC;
		}
	}
}
